package com.yanqu.road.server.cmd.activity.pixiu;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.PiXiuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.pixiu.PiXiuModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.pixiu.PiXiuActivityMgr;
import com.yanqu.road.server.manger.activity.pixiu.PiXiuConfig;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_PI_XIU_SELECT_REWARD, desc = "自选奖励")
public class PiXiuSelectRewardCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 没有活动
        PiXiuConfig config = PiXiuActivityMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        PiXiuProto.PiXiuSelectRewardReqMsg reqMsg = PiXiuProto.PiXiuSelectRewardReqMsg.parseFrom(packet.getBytes());
        if(reqMsg.getReward1() <= 0 || reqMsg.getReward2() <= 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        ret = player.getModule(PiXiuModule.class).selectReward(reqMsg.getReward1(), reqMsg.getReward2());
    }

    @Override
    public void sendData(GamePlayer player) {

        if(ret != 0) {
            player.sendPacket(Protocol.U_PI_XIU_SELECT_REWARD, PiXiuProto.PiXiuSelectRewardRespMsg.newBuilder().setRet(ret));
        }

    }
}
